/*
 * Copyright  2008 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 *
 */
package com.sun.lwuit.uidemo;

import com.sun.dtv.lwuit.Button;
import com.sun.dtv.lwuit.Container;
import com.sun.dtv.lwuit.events.ActionEvent;
import com.sun.dtv.lwuit.events.ActionListener;
import com.sun.dtv.lwuit.layouts.BorderLayout;
import com.sun.dtv.lwuit.layouts.BoxLayout;
import com.sun.dtv.lwuit.layouts.CoordinateLayout;
import com.sun.dtv.lwuit.layouts.FlowLayout;
import com.sun.dtv.lwuit.layouts.GridLayout;

/**
 * 
 * @author cf130546
 */
public class LayoutDemo extends Demo {

	private Button border;

	private Button boxY;

	private Button boxX;

	private Button flow;

	private Button grid;

	private Button coordinate;

	public void cleanup() {
		border = null;
		boxY = null;
		flow = null;
		grid = null;
		boxX = null;
		coordinate = null;
	}

	public String getName() {
		return "Layouts";
	}

	protected String getHelp() {
		return "The toolkit supports 5 different Layouts: FlowLayout, BorderLayout, BoxLayout, GridLayout and GroupLayout."
				+ "In this Demo we added 5 Components to the Form and we changes the Layout and rearrange the Components"
				+ "A developer can add his own Layout by extending the Layout class";
	}

	protected void execute(final Container f) {
		f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		border = new Button("BorderLayout");
		border.getStyle().setBgTransparency(100);

		border.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				f.setLayout(new BorderLayout());
				f.removeAll();
				f.setScrollable(false);
				f.addComponent(BorderLayout.NORTH, border);
				f.addComponent(BorderLayout.EAST, boxY);
				f.addComponent(BorderLayout.CENTER, grid);
				f.addComponent(BorderLayout.WEST, flow);
				f.addComponent(BorderLayout.SOUTH, boxX);
				f.getComponentForm().show();
			}
		});
		boxY = new Button("BoxLayout-Y");
		boxY.getStyle().setBgTransparency(100);
		boxY.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
				f.setScrollable(false);
				addComponents(f);
				f.getComponentForm().show();
			}
		});
		flow = new Button("FlowLayout");
		flow.getStyle().setBgTransparency(100);
		flow.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				f.setLayout(new FlowLayout());
				f.setScrollable(false);
				addComponents(f);
				f.getComponentForm().show();
			}
		});

		grid = new Button("GridLayout");
		grid.getStyle().setBgTransparency(100);
		grid.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				f.setLayout(new GridLayout(3, 2));
				f.setScrollable(false);
				addComponents(f);
				f.getComponentForm().show();
			}
		});

		boxX = new Button("BoxLayout-X");
		boxX.getStyle().setBgTransparency(100);
		boxX.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				f.setLayout(new BoxLayout(BoxLayout.X_AXIS));
				f.setScrollable(true);
				addComponents(f);
				f.getComponentForm().show();
			}
		});

		coordinate = new Button("CoordinateLayout");
		coordinate.getStyle().setBgTransparency(100);

		coordinate.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				f.setLayout(new CoordinateLayout(500, 500));

				boxY.setX(0);
				boxX.setX(30);
				border.setX(60);
				flow.setX(90);
				grid.setX(120);
				coordinate.setX(150);

				boxY.setY(0);
				boxX.setY(30);
				border.setY(60);
				flow.setY(90);
				grid.setY(120);
				coordinate.setY(150);

				f.setScrollable(true);
				addComponents(f);
				f.getComponentForm().show();
			}
		});

		addComponents(f);
		f.getComponentForm().show();
	}

	private void addComponents(final Container f) {
		f.removeAll();
		f.addComponent(boxY);
		f.addComponent(boxX);
		f.addComponent(border);
		f.addComponent(flow);
		f.addComponent(grid);
		f.addComponent(coordinate);
	}
}
